/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import javax.servlet.http.HttpServlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.text.*;

/**
 * @author JS: Juan Sanjuan
 * This Class is used to read the image map generated
 * from sistogeo
 */
public class SistogeoMap extends HttpServlet
{

    public void init() throws ServletException
    {
    }

    public void doPost(
            javax.servlet.http.HttpServletRequest request,
            javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException, java.io.IOException
    {
    }

    public void doGet(
            javax.servlet.http.HttpServletRequest request,
            javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException, java.io.IOException
    {
        String myID = (String) request.getParameter("ID");
        try
        {
            //primeramente, comprobamos que se ha pasado el ID, si no existe no se devuelve foto                          
            if (myID == null)
            {
                throw new Exception("[utils.imagenes.ObtenerImagen]Parametro tiene un valor desconocido");
            } else
            {
                try
                {
                    //leemos el fichero del ftp,usando el metodo que traduce el path
                    String strRuta = "c:\\programacion\\" + myID + ".jpg";
                    File f = new File(strRuta);
                    RandomAccessFile raf = new RandomAccessFile(strRuta, "r");
                    FileInputStream fis = new FileInputStream(f);
                    FileReader fr = new FileReader(f);
                    int i;
                    byte b[] = new byte[(int) f.length()];
                    raf.read(b);
                    //cabecera
                    response.setHeader("Content-Type", "img/jpeg");
                    response.setIntHeader("Content-Length", (int) f.length());
                    //response.setHeader("Accept-Ranges", "bytes");
                    //lo escribimos
                    OutputStream out = response.getOutputStream();
                    out.write(b);
                    out.close();
                    raf.close();
                    fis.close();
                    fr.close();
                } catch (FileNotFoundException fe)
                {
                    throw new Exception();
                }
            }
        } catch (Exception e)
        {
            System.out.println(e.getMessage());
        } //catch
    } //doGet          
}
